//
//  TopScrollView.m
//  SweetComics
//
//  Created by LeeZhe on 15/2/3.
//  Copyright (c) 2015年 LeeZhe. All rights reserved.
//

#import "TopScrollView.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ComicList.h"
@implementation TopScrollView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (void)createView
{
    //最上面的scrollView
    self.scrollViewLittle = [[UIScrollView alloc]initWithFrame:self.frame];
    self.scrollViewLittle.delegate = self;
    self.scrollViewLittle.pagingEnabled = YES;
    _scrollViewLittle.contentSize = CGSizeMake(self.bounds.size.width * 3, 0);
    
    [self addSubview:_scrollViewLittle];
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.bounds.size.width / 2.84, self.bounds.size.height - 15, self.bounds.size.width / 3.787, self.bounds.size.height / 11.36)];
    [self addSubview:self.pageControl];
    [self showPageControl];
    _flag = 0;
    //上面的scrollView里的画面能自己跳动
    [self ChangeContentoffset];
    [self netWork];
    [self change];
    [self.pageControl release];
}

- (void)netWork
{
    self.dic = [NSMutableDictionary dictionary];
    self.arr = [NSMutableArray array];
    //    [self createMBView];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:kTopUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _dic = responseObject;
        NSMutableArray *dataArr = [[[_dic objectForKey:@"data"] objectForKey:@"banner"] objectForKey:@"data"];
   
        if (dataArr) {
            // 给三张 imageView赋值
            self.imageViewFirst = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
            NSString *str1 = [[[[[_dic objectForKey:@"data"]objectForKey:@"banner"]objectForKey:@"data"] objectAtIndex:0] objectForKey:@"banner_url"];
            NSURL *url = [NSURL URLWithString:str1];
            [self.imageViewFirst setImageWithURL:url];
            [self.scrollViewLittle addSubview:self.imageViewFirst];
            self.countOne =  [[[[[_dic objectForKey:@"data"]objectForKey:@"banner"]objectForKey:@"data"] objectAtIndex:0] objectForKey:@"comic_id"];
            [self netWorkTwo];

            [_imageViewFirst release];
            //第二张
            self.imageViewSeconcd = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
            NSString *str2 = [[[[[_dic objectForKey:@"data"]objectForKey:@"banner"]objectForKey:@"data"] objectAtIndex:1] objectForKey:@"banner_url"];
            NSURL *urlTwo = [NSURL URLWithString:str2];
            [self.imageViewSeconcd setImageWithURL:urlTwo];
            //        self.imageViewSeconcd.backgroundColor = [UIColor redColor];
            [self.scrollViewLittle addSubview:self.imageViewSeconcd];
            self.countTwo = [[dataArr objectAtIndex:1]  objectForKey:@"comic_id"];
            [self netWorkThree];

            [_imageViewSeconcd release];
            //第三张
            self.imageViewThird = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width * 2, 0, self.bounds.size.width, self.bounds.size.height)];
            NSString *str3 = [[[[[_dic objectForKey:@"data"]objectForKey:@"banner"]objectForKey:@"data"] objectAtIndex:2] objectForKey:@"banner_url"];
            NSURL *urlThree = [NSURL URLWithString:str3];
            [self.imageViewThird setImageWithURL:urlThree];
            //        imageView3.backgroundColor = [UIColor redColor];
            [self.scrollViewLittle addSubview:self.imageViewThird];
            self.countThree =  [[dataArr objectAtIndex:2] objectForKey:@"comic_id"];
            [self netWorkFour];

            [_imageViewThird release];
        }else {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"亲好像没有网络\r甜心暂时偷懒了哦" delegate: self cancelButtonTitle:@"好的" otherButtonTitles:nil];
            [alertView show];
            [self addSubview:alertView];
            [alertView release];
        }
        
        
        //        [_mbView hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)netWorkTwo
{
//    NSLog(@"!~~~~!!%@",_countOne);
    NSString *url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/ComicDetailForIos/comic_id/%@/page/1/listcnt/9999/uin/0/local_version/2.3.1/channel/1001/guest_id/(null)",_countOne];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *dic =[responseObject objectForKey:@"data"] ;
        //                        NSMutableArray *arr = [dic objectForKey:@"data"];
        //            NSMutableArray *comicsArr = [NSMutableArray array];
        ComicList *comic = [[ComicList alloc] init];
        [comic setValuesForKeysWithDictionary:dic];
        
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"One" object:comic userInfo:nil];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
}
- (void)netWorkThree
{
   NSString *url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/ComicDetailForIos/comic_id/%@/page/1/listcnt/9999/uin/0/local_version/2.3.1/channel/1001/guest_id/(null)",_countTwo];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        ComicList *comic = [[ComicList alloc] init];
        [comic setValuesForKeysWithDictionary:dic];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Two" object:comic];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
}
- (void)netWorkFour
{
  NSString *url = [NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Comic/ComicDetailForIos/comic_id/%@/page/1/listcnt/9999/uin/0/local_version/2.3.1/channel/1001/guest_id/(null)",_countThree];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];
        ComicList *comic = [[ComicList alloc] init];
        [comic setValuesForKeysWithDictionary:dic];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Three" object:comic];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
    
}



-(void)showPageControl
{
    _pageControl.numberOfPages = 3;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    [self addSubview:_pageControl];
    
}

- (void)ChangeContentoffset
{
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(change) userInfo:nil repeats:YES];
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];
    [timer fire];
}

- (void)change
{
    _scrollViewLittle.contentOffset = CGPointMake(_scrollViewLittle.contentOffset.x + _scrollViewLittle.frame.size.width * _flag, 0);
    _flag++;
    if (_flag == 2) {
        _flag = 1;
    }
    
    if (_scrollViewLittle.contentOffset.x == self.frame.size.width*3) {
        _scrollViewLittle.contentOffset = CGPointMake(0, 0);
    }
    
}

- (void)ChangeContentoffsetAction1:(NSTimer *)timer
{
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = self.scrollViewLittle.contentOffset.x / self.scrollViewLittle.frame.size.width;
    self.pageControl.currentPage = page;
}
@end
